package com.suda.sudaexamsystem.service.impl;

import com.suda.sudaexamsystem.mapper.NoticeMapper;
import com.suda.sudaexamsystem.mapper.NoticeUserMapper;
import com.suda.sudaexamsystem.pojo.Notice;
import com.suda.sudaexamsystem.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class NoticeServiceImpl implements NoticeService {
    @Autowired
    private NoticeMapper noticeMapper;
    @Autowired
    private NoticeUserMapper noticeUserMapper;

    @Override
    @Transactional
    public void publishNotice(Notice notice) {
        noticeMapper.publishNotice(notice);
        // 设置可见用户
        noticeUserMapper.addUserToVisitBatch(notice.getUserList(), notice.getId());
    }

    @Override
    @Transactional
    public void updateNotice(Notice notice) {
        noticeMapper.updateNotice(notice);
    }

    @Override
    @Transactional
    public void deleteById(int noticeId) {
        // 删除公告和用户的关联
        noticeUserMapper.invisibleByNoticeId(noticeId);
        noticeMapper.deleteById(noticeId);
    }

    @Override
    @Transactional
    public Notice findById(int noticeId) {
        return noticeMapper.findById(noticeId);
    }

    @Override
    @Transactional
    public List<Notice> findNoticesByUserId(int userId) {
        return noticeMapper.findNoticesByUserId(userId);
    }
}
